import { reactive, onMounted, ref, nextTick } from 'vue'
import { StageTable, AddModel } from '@/api/01work/01type'
import { taskpageApi } from '@/api/ReviewBusiness/01Project/project'
import { dictPagePageApi } from '@/api/dictionary/dictionary'
import { TaskPageParams, TaskTable } from '@/api/ReviewBusiness/01Project/projectType'
import { stagePageApi } from '@/api/01work/01stage'
import { StageList } from '@/api/01work/01type'
import { Result, StatusCode } from '@/utils/request';
import { EditType } from '@/type/BaseEnum'
import useInstance from '@/hooks/useInstance';
export default function ProjectTable() {
  const { global, proxy } = useInstance()
  const ProjectData = reactive<TaskTable>({
    list: [],
    Loading: false,
    height: 0,
    total: 0
  })
  const dictDateList = ref<any>({})
  // 查询参数
  let searchParams = reactive<TaskPageParams>({
    page: 1,
    size: 10,
    name: '',
    sort: ''
  })
  const stageParams = reactive<StageList>({
    page: 1,
    size: 100,
    name: ''
  })
  onMounted(() => {
    getTableList()
    getDictList()
    getStageList()
    nextTick(() => {
      ProjectData.height = window.innerHeight - 185
    })
  })
  let stageTableData = reactive<any>({
    list: [],
    table: [],
    Loading: false
  })
  const getDictList = async () => {
    const params = {
      name: '',
      code: '',
      page: 1,
      size: 10,
      sort: ''
    }
    const res = await dictPagePageApi(params)
    if (res.code === 200) {
      // 处理数据 让code 作为建 来处理数据
      // dictDateList.value = res.data.records
      for (var k in res.data && res.data.records) {
        dictDateList.value[res.data.records[k].code] = res.data.records[k]
      }    
    }
  }
  const getTableList = async () => {
    ProjectData.Loading = true
    const res = await taskpageApi(searchParams)
    if (res.code === 200) {
      ProjectData.Loading = false
      ProjectData.list = res.data.records
      ProjectData.total = res.data.total
    }
  }
  const getStageList = async () => {
    const res = await stagePageApi(stageParams)
    console.log(res.data);
    
    if (res.code === 200) {
      stageTableData.list = res.data.records
    }
  }
  const handleSizeChange = (size) => {
    searchParams.size = size
    getTableList()
  }
  const tableRowClassName = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 1) {
      return "warning-row";
    } else {
      return "success-row";
    }
  }
  const handleCurrentChange = (current) => {
    searchParams.page = current
    getTableList()
  }
  return {
    ProjectData, getTableList, searchParams, dictDateList, handleSizeChange, handleCurrentChange, stageTableData,tableRowClassName
  }
}
